﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using PGMHelper;
using Newtonsoft.Json;
using System.Windows.Forms.DataVisualization.Charting;

namespace Spectrum
{
    /// <summary>
    /// 类型
    /// </summary>
    public enum MotionEnum
    {
        /// <summary>
        /// 加速度
        /// </summary>
        Accel = 1,

        /// <summary>
        /// 速度
        /// </summary>
        Vel = 2,

        /// <summary>
        /// 位移
        /// </summary>
        Disp = 3,
    }


    [JsonObject(MemberSerialization.OptIn)]
    /// <summary>
    /// 时间参数
    /// </summary>
    public class AccelParam
    {
        [JsonProperty]
        /// <summary>
        /// 时间
        /// </summary>
        public float Time { set; get; }

        [JsonProperty]
        /// <summary>
        /// 加速度
        /// </summary>
        public float Accel { set; get; }

        /// <summary>
        /// 速度
        /// </summary>
        public float Vel { set; get; }

        /// <summary>
        /// 位移
        /// </summary>
        public float Disp { set; get; }

        /// <summary>
        /// 获得激励值
        /// </summary>
        /// <param name="motionEnum"></param>
        /// <returns></returns>
        public float GetValue(MotionEnum motionEnum)
        {
            switch(motionEnum)
            {
                case MotionEnum.Accel: return this.Accel;
                case MotionEnum.Disp: return this.Disp;
                case MotionEnum.Vel: return this.Vel;
                default:return this.Accel;
            }
        }

        /// <summary>
        /// 设定地震波的速度和位移曲线
        /// </summary>
        /// <param name="paramI"></param>
        public void SetVelDisp(AccelParam paramI)
        {
            this.Vel = paramI.Vel + (this.Accel + paramI.Accel) * (this.Time - paramI.Time) * 0.5F;
            this.Disp = paramI.Disp + (this.Vel + paramI.Vel) * (this.Time - paramI.Time) * 0.5F;
        }

        /// <summary>
        /// 线性插值
        /// </summary>
        /// <param name="paramJ"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public AccelParam Interpolation(AccelParam paramJ, float time)
        {
            //求得斜率
            float slope = (paramJ.Accel - this.Accel) / (paramJ.Time - this.Time);
            //线性插值求得加速度
            float accel = slope * (time - this.Time) + this.Accel;
            //返回参数
            return new AccelParam
            {
                Time = time,
                Accel = accel,
            };
        }

        /// <summary>
        /// 空的构造函数
        /// </summary>
        public AccelParam()
        {
            this.Accel = 0;
            this.Vel = 0;
            this.Disp = 0;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Time"></param>
        public AccelParam(float time): this()
        {
            this.Time = time;
        }
    }
}
